package me.zk.helloworld;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Hello world!
 *
 */
public class RabbitMqConsumerDemo {
    public static void main( String[] args ) throws IOException, TimeoutException {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        // 获取connection
        Connection connection = factory.newConnection();
        // 获取channel
        Channel channel = connection.createChannel();
        //使用对默认交换机
        channel.queueDeclare("hello1",true,false,false,null);

        //
        channel.basicConsume("hello1", true, new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                System.out.println("消息内容："+new String(delivery.getBody()));
            }
        }, new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {
                System.out.println("CancelCallback");
            }
        });
        channel.close();
        connection.close();
    }
}
